{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "# 5.4 用梯度下降法求极值"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "对于二元凸函数$f(x,y)=x^2+y^2+2$，用梯度下降法求函数的极小值。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f5b4fc39-cbcf-432a-bf1e-e75e642d4b87",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "127f3dfb-8f5c-4e5b-b5ab-d6c33425f3d1",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 0 轮: 调节前的x: 3.0000 ,y: 3.0000 ,df_dx:  6.0000 ,df_dy: 6.0000\n",
      "本轮调整后的x为: 1.2000 ,y为: 1.2000 ,f为: 4.8800\n",
      "第 1 轮: 调节前的x: 1.2000 ,y: 1.2000 ,df_dx:  2.4000 ,df_dy: 2.4000\n",
      "本轮调整后的x为: 0.4800 ,y为: 0.4800 ,f为: 2.4608\n",
      "第 2 轮: 调节前的x: 0.4800 ,y: 0.4800 ,df_dx:  0.9600 ,df_dy: 0.9600\n",
      "本轮调整后的x为: 0.1920 ,y为: 0.1920 ,f为: 2.0737\n",
      "第 3 轮: 调节前的x: 0.1920 ,y: 0.1920 ,df_dx:  0.3840 ,df_dy: 0.3840\n",
      "本轮调整后的x为: 0.0768 ,y为: 0.0768 ,f为: 2.0118\n",
      "第 4 轮: 调节前的x: 0.0768 ,y: 0.0768 ,df_dx:  0.1536 ,df_dy: 0.1536\n",
      "本轮调整后的x为: 0.0307 ,y为: 0.0307 ,f为: 2.0019\n",
      "第 5 轮: 调节前的x: 0.0307 ,y: 0.0307 ,df_dx:  0.0614 ,df_dy: 0.0614\n",
      "本轮调整后的x为: 0.0123 ,y为: 0.0123 ,f为: 2.0003\n",
      "第 6 轮: 调节前的x: 0.0123 ,y: 0.0123 ,df_dx:  0.0246 ,df_dy: 0.0246\n",
      "本轮调整后的x为: 0.0049 ,y为: 0.0049 ,f为: 2.0000\n",
      "第 7 轮: 调节前的x: 0.0049 ,y: 0.0049 ,df_dx:  0.0098 ,df_dy: 0.0098\n",
      "本轮调整后的x为: 0.0020 ,y为: 0.0020 ,f为: 2.0000\n",
      "第 8 轮: 调节前的x: 0.0020 ,y: 0.0020 ,df_dx:  0.0039 ,df_dy: 0.0039\n",
      "本轮调整后的x为: 0.0008 ,y为: 0.0008 ,f为: 2.0000\n",
      "第 9 轮: 调节前的x: 0.0008 ,y: 0.0008 ,df_dx:  0.0016 ,df_dy: 0.0016\n",
      "本轮调整后的x为: 0.0003 ,y为: 0.0003 ,f为: 2.0000\n",
      "第 10 轮: 调节前的x: 0.0003 ,y: 0.0003 ,df_dx:  0.0006 ,df_dy: 0.0006\n",
      "本轮调整后的x为: 0.0001 ,y为: 0.0001 ,f为: 2.0000\n",
      "第 11 轮: 调节前的x: 0.0001 ,y: 0.0001 ,df_dx:  0.0003 ,df_dy: 0.0003\n",
      "本轮调整后的x为: 0.0001 ,y为: 0.0001 ,f为: 2.0000\n",
      "第 12 轮: 调节前的x: 0.0001 ,y: 0.0001 ,df_dx:  0.0001 ,df_dy: 0.0001\n",
      "本轮调整后的x为: 0.0000 ,y为: 0.0000 ,f为: 2.0000\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "# x的初始值\n",
    "x=tf.Variable(3.)\n",
    "y=tf.Variable(3.)\n",
    "# 迭代次数\n",
    "iter=13\n",
    "# 学习率\n",
    "lr=0.3\n",
    "for i in range(0,iter):\n",
    "    with tf.GradientTape() as tape:\n",
    "        f=tf.square(x)+tf.square(y)+2        \n",
    "#     计算偏导数\n",
    "    df_dx,df_dy=tape.gradient(f,[x,y])    \n",
    "    print('第',i,'轮:','调节前的x:','%.4f'%x,',y:','%.4f'%y,',df_dx: ', '%.4f'%df_dx,',df_dy:','%.4f'%df_dy)\n",
    "#     更新x和y\n",
    "    x.assign_sub(lr*df_dx)\n",
    "    y.assign_sub(lr*df_dy)\n",
    "#     更新后的函数值y\n",
    "    f=tf.square(x)+tf.square(y)+2    \n",
    "    print('本轮调整后的x为:','%.4f'%x,',y为:','%.4f'%y,',f为:','%.4f'%f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "947bd149-004c-4238-863f-ba125a57b79b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
